package com.css.dzj.dzjdzqs.modules.apaction.aprecordandgain.service.impl;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjdzqs.common.util.HttpClientUtil;
import com.css.dzj.dzjdzqs.modules.apaction.aprecordandgain.entity.JgJgdxCdcsPlaceEntity;
import com.css.dzj.dzjdzqs.modules.apaction.aprecordandgain.mapper.JgJgdxCdcsPlaceMapper;
import com.css.dzj.dzjdzqs.modules.apaction.aprecordandgain.service.JgJgdxCdcsPlaceService;
import com.css.dzj.dzjdzqs.modules.apaction.aprecordandgain.vo.ApRecordEntityVo;
import com.css.dzj.dzjdzqs.modules.apaction.aprecordandgain.vo.ApRecordGainEntityVo;
import com.css.dzj.dzjdzqs.modules.apinventory.service.ApInventService;
import com.css.dzj.dzjdzqs.modules.datafromzfjg.service.JgLhjgJcdxService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class JgJgdxCdcsPlaceServiceImpl implements JgJgdxCdcsPlaceService {
    @Autowired
    private JgLhjgJcdxService jcdxService;
    @Value("${AP_URL}")
    private String apUrl;
    @Value("${JCSS_URL}")
    private String jcssUrl;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private JgJgdxCdcsPlaceMapper jgJgdxCdcsPlaceMapper;
    @Autowired
    private ApInventService apInventService;

    //获取来自安评备案信息数据
    @Override
    public void addApRecord()  {
        try {
            String list = HttpClientUtil.doGet(apUrl + "/aprecord/queryAllRecordMsg");
            List<ApRecordEntityVo> results = JSON.parseArray(list, ApRecordEntityVo.class);
            for (ApRecordEntityVo result : results) {
                JgJgdxCdcsPlaceEntity jgJgdxCdcsPlaceEntity = new JgJgdxCdcsPlaceEntity();
                jgJgdxCdcsPlaceEntity.setPlaceId(result.getRecordId());
                jgJgdxCdcsPlaceEntity.setPlaceName(result.getRecordProjectName());
                jgJgdxCdcsPlaceEntity.setArea(result.getRecordProjectAddress());
                //TODO 查询所在地区行政区划代码
                jgJgdxCdcsPlaceEntity.setAreaNumber(apInventService.findByAddress(result.getRecordProjectAddress()).getAdministrativeArea());
                jgJgdxCdcsPlaceEntity.setResponsibleDept(result.getRecordProjectUnitname());
                jgJgdxCdcsPlaceEntity.setEnterpriseCreditCode(result.getCommitmentUnitCode());
                jgJgdxCdcsPlaceEntity.setChargePerson(result.getRecordProjectLegal());
                jgJgdxCdcsPlaceEntity.setCertificateType(result.getCertificateType());
                jgJgdxCdcsPlaceEntity.setCertificateNumber(result.getCertificateNumber());
                jgJgdxCdcsPlaceEntity.setContactTel(result.getRecordProjectPhone());
                jgJgdxCdcsPlaceEntity.setPlacePurpose(result.getPlacePurpose());
                jgJgdxCdcsPlaceEntity.setPlaceScale(result.getPlaceScale());
                jgJgdxCdcsPlaceEntity.setStartDate(result.getStartDate());
                jgJgdxCdcsPlaceEntity.setDeviceLifetime(result.getDeviceLifetime());
                jgJgdxCdcsPlaceEntity.setDataFrom("1");
                jgJgdxCdcsPlaceMapper.save(jgJgdxCdcsPlaceEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //获取来自区域地震安全性评估成果/地震小区划工作成果-备案信息表 数据
    @Override
    public void addApRecordGain()  {
        try {
            String list = HttpClientUtil.doGet(apUrl + "/recordgain/queryAllRecordGain");
            List<ApRecordGainEntityVo> results = JSON.parseArray(list, ApRecordGainEntityVo.class);
            for (ApRecordGainEntityVo result : results) {
                JgJgdxCdcsPlaceEntity jgJgdxCdcsPlaceEntity = new JgJgdxCdcsPlaceEntity();
                jgJgdxCdcsPlaceEntity.setPlaceId(result.getRecordId());
                jgJgdxCdcsPlaceEntity.setPlaceName(result.getRecordProjectAddress());
                jgJgdxCdcsPlaceEntity.setArea(result.getRecordProjectAddress());
                //TODO 查询所在地区行政区划代码
                jgJgdxCdcsPlaceEntity.setAreaNumber("");
                jgJgdxCdcsPlaceEntity.setResponsibleDept(result.getRecordProjectUnitname());
                jgJgdxCdcsPlaceEntity.setEnterpriseCreditCode(result.getCommitmentUnitCode());
                jgJgdxCdcsPlaceEntity.setChargePerson(result.getRecordProjectLegal());
                jgJgdxCdcsPlaceEntity.setCertificateType(result.getCertificateType());
                jgJgdxCdcsPlaceEntity.setCertificateNumber(result.getCertificateNumber());
                jgJgdxCdcsPlaceEntity.setContactTel(result.getRecordProjectPhone());
                jgJgdxCdcsPlaceEntity.setPlacePurpose(result.getPlacePurpose());
                jgJgdxCdcsPlaceEntity.setPlaceScale(result.getPlaceScale());
                jgJgdxCdcsPlaceEntity.setStartDate(result.getStartDate());
                jgJgdxCdcsPlaceEntity.setDeviceLifetime(result.getDeviceLifetime());
                jgJgdxCdcsPlaceEntity.setDataFrom("2");
                jgJgdxCdcsPlaceMapper.save(jgJgdxCdcsPlaceEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //获取来自监测实施的系统场地场所信息
    @Override
    public void addJcssRecordGain()  {
        try {
            String list = HttpClientUtil.doGet(jcssUrl + "/recordgain/queryAllRecordGain");
            List<JgJgdxCdcsPlaceEntity> results = JSON.parseArray(list, JgJgdxCdcsPlaceEntity.class);
            for (JgJgdxCdcsPlaceEntity result : results) {
                JgJgdxCdcsPlaceEntity jgJgdxCdcsPlaceEntity = new JgJgdxCdcsPlaceEntity();
                jgJgdxCdcsPlaceEntity.setPlaceId(result.getPlaceId());
                jgJgdxCdcsPlaceEntity.setPlaceName(result.getPlaceName());
                jgJgdxCdcsPlaceEntity.setArea(result.getArea());
                jgJgdxCdcsPlaceEntity.setAreaNumber(result.getAreaNumber());
                jgJgdxCdcsPlaceEntity.setResponsibleDept(result.getResponsibleDept());
                jgJgdxCdcsPlaceEntity.setEnterpriseCreditCode(result.getEnterpriseCreditCode());
                jgJgdxCdcsPlaceEntity.setChargePerson(result.getChargePerson());
                jgJgdxCdcsPlaceEntity.setCertificateType(result.getCertificateType());
                jgJgdxCdcsPlaceEntity.setCertificateNumber(result.getCertificateNumber());
                jgJgdxCdcsPlaceEntity.setContactTel(result.getContactTel());
                jgJgdxCdcsPlaceEntity.setPlacePurpose(result.getPlacePurpose());
                jgJgdxCdcsPlaceEntity.setPlaceScale(result.getPlaceScale());
                jgJgdxCdcsPlaceEntity.setStartDate(result.getStartDate());
                jgJgdxCdcsPlaceEntity.setDeviceLifetime(result.getDeviceLifetime());
                jgJgdxCdcsPlaceMapper.save(jgJgdxCdcsPlaceEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
